package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * dao层做
 * 1.sql语句
 * 2.返回结果值
 */
public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (name) values('" + productModel.getName() + "')";
        return JDBC.update(sql);
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "select count(*) count from product";
        return JDBC.getCount(sql);
    }

    @Override
    public List<ProductModel> findAll(ProductModel model) {
        String sql = "select * from \n" +
                "(select p.*,c.name cuisineName from product p\n" +
                "left join cuisine c\n" +
                "on p.cuisine_id=c.id ) t\n" +
                "where  1=1 ";
        sql += getSql(model);
        sql += " order by id desc ";
        sql += " limit " + (model.getPage() - 1) * model.getLimit() + "," + model.getLimit() + "";
        return getList(sql);
    }

    private String getSql(ProductModel model) {
        String sql = "";
        if (!"".equals(model.getName())) {
            sql += " and name like '%" + model.getName() + "%'";
        }
        return sql;
    }

    private List<ProductModel> getList(String sql) {
        List<ProductModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setMarketPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                try {
                    model.setCuisineName(resultSet.getString("cuisineName"));
                } catch (Exception e) {

                }
                model.setIsShow(resultSet.getInt("is_show"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        String sql = "select * from product where id=" + productModel.getId();
        List<ProductModel> list = getList(sql);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int delete(ProductModel productModel) {
        return 0;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update product set name='" + productModel.getName() + "' where id=" + productModel.getId();
        return JDBC.update(sql);
    }
}
